package com.enqualcomm.kids.mvp.seetheworld;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.config.ImageUriFactory;
import com.facebook.common.util.UriUtil;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ximalaya.ting.android.opensdk.model.tag.Tag;

import java.util.List;

/**
 * Created by Sunzhigang on 2016/3/30.
 */
public class XmlyChildTagAdapter extends BaseAdapter {
    private List<Tag> tagList;
    private int position;
    private Context context;
    private int[] resId = {
            R.drawable.xmly_one,R.drawable.xmly_two,R.drawable.xmly_three,R.drawable.xmly_four,
            R.drawable.xmly_five,R.drawable.xmly_six,R.drawable.xmly_seven,R.drawable.xmly_eight,
            R.drawable.xmly_nine
    };

    public XmlyChildTagAdapter(Context context) {
        this.context = context;
    }

    public void setTagData(List<Tag> tagList) {
        if (tagList != null) {
            this.tagList = tagList;
            notifyDataSetChanged();
        }
    }

    public void clearData(){
        if (tagList != null){
            tagList.clear();
            notifyDataSetChanged();
        }
    }

    public void setTagItemSelect(int position) {
        this.position = position;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return tagList == null ? 0 : tagList.size();
    }

    @Override
    public Object getItem(int i) {
        return tagList == null ? null : tagList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.child_tags_item, null);
            viewHolder.category_sdv = (SimpleDraweeView) view.findViewById(R.id.category_sdv);
            viewHolder.name_tv = (TextView) view.findViewById(R.id.name_tv);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }


        Uri uri = ImageUriFactory.getResUri(resId[i]);
        viewHolder.category_sdv.setImageURI(uri);
//        viewHolder.category_sdv.setBackgroundResource(resId[i]);
        final String tagName = tagList.get(i).getTagName();
        if (tagName.equals("中小学教材")){
            viewHolder.name_tv.setText("小学教材");
        }else {
            viewHolder.name_tv.setText(tagName);
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tagClickListenr.OnTagClick(tagName);
                setTagItemSelect(i);
            }
        });
        if (i==position){
            viewHolder.category_sdv.setSelected(true);
        }else {
            viewHolder.category_sdv.setSelected(false);
        }
        return view;
    }

    private static class ViewHolder {
        public SimpleDraweeView category_sdv;
        public TextView name_tv;
    }

    /**
     * Tag点击
     */
    private OnTagClickListenr tagClickListenr;

    public void setOnTagClickListenr(OnTagClickListenr tagClickListenr) {
        this.tagClickListenr = tagClickListenr;
    }

    public interface OnTagClickListenr {
        void OnTagClick(String tagName);
    }
}
